import 'package:dio/dio.dart';
import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:jyofservice/bean/work_condition_entity.dart';
import 'package:jyofservice/util/AesUtil.dart';
import 'package:jyofservice/util/CallRecordNotification.dart';

class WorkCondition extends StatefulWidget {
  String? getyunxinggongkuangbyhouseid;

  @override
  _WorkConditionState createState() => _WorkConditionState();
}

class _WorkConditionState extends State<WorkCondition> {
  ScrollController scrollController = ScrollController();
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  bool isloading = false;
  List<WorkConditionEntity> posts = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getWorkCondition();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        elevation: 0.5,
        centerTitle: true,
        title: Text('维修公告',
            style: TextStyle(
                color: Colors.white, fontSize: ScreenUtil().setSp(45))),
      ),
      body: Container(
          color: const Color(0xFFF2F5F9),
          child: Stack(
            alignment: Alignment(0.5, 0.5),
            children: <Widget>[
              RefreshIndicator(
                  child: ListView(
                    //不能使用否则下拉刷新也会调用  scrollController.addListener(() 方法
//              physics: AlwaysScrollableScrollPhysics(),
                    controller: scrollController,
                    children: <Widget>[
                      SingleChildScrollView(
                        scrollDirection: Axis.horizontal,
                        child: DataTable(
//                Datacollen (label: Container (child: Text ('2011') ，color: Colors.grey,)
                            columns: [
                              DataColumn(
                                  label: Text(
                                '标题',
                                style: TextStyle(
                                    color: Colors.blue,
                                    fontSize: ScreenUtil().setSp(38),
                                    fontWeight: FontWeight.bold),
                              )),
                              DataColumn(
                                  label: Text(
                                '内容',
                                style: TextStyle(
                                    color: Colors.blue,
                                    fontSize: ScreenUtil().setSp(38),
                                    fontWeight: FontWeight.bold),
                              )),
                              DataColumn(
                                  label: Text(
                                '范围',
                                style: TextStyle(
                                    color: Colors.blue,
                                    fontSize: ScreenUtil().setSp(38),
                                    fontWeight: FontWeight.bold),
                              )),
                              DataColumn(
                                  label: Text(
                                '截至时间',
                                style: TextStyle(
                                    color: Colors.blue,
                                    fontSize: ScreenUtil().setSp(38),
                                    fontWeight: FontWeight.bold),
                              )),
                              DataColumn(
                                  label: Text(
                                '创建时间',
                                style: TextStyle(
                                    color: Colors.blue,
                                    fontSize: ScreenUtil().setSp(38),
                                    fontWeight: FontWeight.bold),
                              )),
                              DataColumn(
                                  label: Text(
                                '创建人',
                                style: TextStyle(
                                    color: Colors.blue,
                                    fontSize: ScreenUtil().setSp(38),
                                    fontWeight: FontWeight.bold),
                              )),
                            ], rows: getTableRow()),
                      )
                    ],
                  ),
                  onRefresh: onHeaderRefresh),
              Offstage(
                offstage: isloading,
                child: SpinKitFadingCircle(
                  color: Colors.blue,
                  size: 50.0,
                ),
              )
            ],
          )),
    );
  }

  //刷新
  Future<Null> onHeaderRefresh() async {
    posts.clear();
    getWorkCondition();
  }

  List<DataRow> getTableRow() {
    List<DataRow> list = [];
    for (int i = 0; i < posts.length; i++) {
      list.add(DataRow(cells: [
        DataCell(Text(
          posts[i].title!,
          style: TextStyle(fontSize: ScreenUtil().setSp(30)),
        )),
        DataCell(Text(
          posts[i].neiRong!,
          style: TextStyle(fontSize: ScreenUtil().setSp(30)),
        )),
        DataCell(Text(
          posts[i].fanWeiName!,
          style: TextStyle(fontSize: ScreenUtil().setSp(30)),
        )),
        DataCell(Text(
          posts[i].endDt!,
          style: TextStyle(fontSize: ScreenUtil().setSp(30)),
        )),
        DataCell(Text(
          posts[i].createDate!,
          style: TextStyle(fontSize: ScreenUtil().setSp(30)),
        )),
        DataCell(Text(
          posts[i].realName!,
          style: TextStyle(fontSize: ScreenUtil().setSp(30)),
        )),
      ]));
    }
    print(list.length);
    return list;
  }

  Future getWorkCondition() async {
    isloading = false;
    if(mounted) {
      setState(() {

      });
    }

    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    posts.clear();
    String aesData = await (AesUtil.encodeBase64());
    String? key = sharedPreferences.getString("key");
    var data = {"key": key, "ClientType": "phone", "ClientKey": aesData};
    Response response = await Dio().post(
        WebConfig().baseUrl + "DB/GetMyDepartmentWorkInfo",
        data: data);
    print("运行工况:$response");
    if(mounted) {
      setState(() {
        for (int i = 0; i < response.data["Data"].length; i++) {

          posts.add(new WorkConditionEntity.fromJson(response.data["Data"][i]));
        }
        isloading = true;
      });
    }


    return response.data;
  }
}
